package com.zhouheng.springbootactiviti.module.controller;

import com.zhouheng.common.base.web.Result;
import com.zhouheng.springbootactiviti.common.pojo.TaskVO;
import com.zhouheng.springbootactiviti.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "流程引擎测试")
@RestController
@RequestMapping("/activiti")
public class ActivitiTestController {

    @Autowired
    private TaskService taskService;

    @ApiOperation("获取指定流程我的任务列表")
    @GetMapping("tasks")
    public Result<List<TaskVO>> taskList(@RequestParam(defaultValue = "ResignationProcess") String processDefinitionKey) {
        String userName = SecurityUtils.getUserName();
        List<Task> claimList = taskService.createTaskQuery().processDefinitionKey(processDefinitionKey).taskAssignee(userName).list();
        List<TaskVO> collect = claimList.stream().map(task ->
                new TaskVO(task.getId(), task.getName(), task.getCreateTime())
        ).collect(Collectors.toList());
        return Result.success(collect);
    }

}
